<?php

namespace app\admin\controller\classroom;

use app\common\controller\Backend;
use think\exception\DbException;
use think\response\Json;

/**
 * 学习记录
 *
 * @icon fa fa-circle-o
 */
class CourseRecord extends Backend
{

    /**
     * CourseRecord模型对象
     * @var \app\admin\model\classroom\CourseRecord
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\classroom\CourseRecord;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     *
     * @param null $ids
     * @return string|Json
     * @throws DbException
     * @throws \think\Exception
     */
    public function index($ids = null)
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->with(['lesson','user'])
            ->where($where);

        if(!empty($ids)){
            $list = $list
                ->where('course_record.course_id',$ids);
        }
        if(!empty($this->request->get('lesson_id'))){
            $list = $list
                ->where('course_record.lesson_id',$this->request->get('lesson_id'));
        }
        $list = $list
            ->order($sort, $order)
            ->paginate($limit);

        foreach ($list as $row) {
            $row->getRelation('lesson')->visible(['title','file_type']);
            $row->getRelation('user')->visible(['nickname']);
            $row->times = $this->formatStudyTimeWithoutZero($row->times);
        }

        $total_times = $this->model
            ->with(['lesson','user'])
            ->where($where);
        if(!empty($ids)){
            $total_times = $total_times->where('course_record.course_id',$ids);
        }
        if(!empty($this->request->get('lesson_id'))){
            $total_times = $total_times
                ->where('course_record.lesson_id',$this->request->get('lesson_id'));
        }
        $total_times = $total_times->sum('course_record.times');

        $result = ['total' => $list->total(), 'rows' => $list->items(), 'extend'=>['total_times'=>$this->formatStudyTimeWithoutZero($total_times)]];
        return json($result);
    }

    private function formatStudyTimeWithoutZero($seconds)
    {
        $days = floor($seconds / (3600 * 24)); // 计算天数
        $seconds %= (3600 * 24); // 剩余秒数计算小时和分钟

        $hours = floor($seconds / 3600); // 计算小时数
        $seconds %= 3600; // 剩余秒数计算分钟和秒

        $minutes = floor($seconds / 60); // 计算分钟数
        $seconds %= 60; // 剩余秒数即为秒数

        // 构建返回的字符串，忽略值为0的部分
        $formattedTime = '';
        if ($days > 0) {
            $formattedTime .= "$days 天 ";
        }
        if ($hours > 0 || $formattedTime !== '') { // 如果前面已经有天数，或者小时数不为0，则显示小时
            $formattedTime .= "$hours 小时 ";
        }
        if ($minutes > 0 || ($formattedTime !== '' && $hours > 0)) { // 如果前面有天或小时，或者分钟不为0，则显示分钟
            $formattedTime .= "$minutes 分钟 ";
        } elseif ($formattedTime === '') { // 如果没有天和小时，直接显示分钟，无论分钟是否为0
            $formattedTime .= "$minutes 分钟 ";
        }

        // 当前逻辑始终显示秒数，除非是0且前面已经有其他单位
        if ($seconds > 0 || ($formattedTime === '' && $hours === 0 && $minutes === 0)) {
            $formattedTime .= "$seconds 秒";
        }

        return trim($formattedTime); // 移除结尾的空格并返回
    }


}










